package com.konsung.ksdc.manager.machine;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.config.NetConfig;
import com.konsung.ksdc.manager.appmain.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 一体机教程Activity
 * Created by Administrator on 2016/3/16 0016.
 */
public class MachineTeachingActivity extends BaseActivity {

    @Bind(R.id.wv_machine_teaching)
    WebView wvMachineTeaching;
    @Bind(R.id.tab_tv_title)
    TextView tvTitle;
    @Bind(R.id.rl_back)
    RelativeLayout back;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_machine_teach);

        ButterKnife.bind(this);
        init();
    }

    @Override
    protected String activityDesc() {
        return getString(R.string.page_machine_teaching);
    }

    /**
     * 初始化一体机教程页面数据
     */
    private void init() {
        String teachingUrl = NetConfig.getServerUrl() + "ksdc-web/public/help" +
                ".htm";
        progressDialog = new ProgressDialog(MachineTeachingActivity.this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage(getString(R.string.dialog_tip_loading));
        progressDialog.setCancelable(true);
        tvTitle.setText("");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        wvMachineTeaching.getSettings().setJavaScriptEnabled(true); // 可用JS
        wvMachineTeaching.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        // 设置可以支持缩放
        wvMachineTeaching.getSettings().setSupportZoom(true);
        // 滚动条风格，为0就是不给滚动条留空间，滚动条覆盖在网页上
        wvMachineTeaching.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap
                    favicon) {
                // 开始时显示
                progressDialog.show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // 结束时消失
                progressDialog.hide();
            }

            @Override
            public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
                // 载入网页
                view.loadUrl(url);
                return true;
            }
        });

        wvMachineTeaching.loadUrl(teachingUrl);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && wvMachineTeaching
                .canGoBack()) {
            wvMachineTeaching.goBack();
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
            this.finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
